package com.luluteam.pushlib.tools;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.luluteam.pushlib.constant.AppHolder;
import com.luluteam.pushlib.constant.CtxConstant;
import com.luluteam.pushlib.model.ContextInfo;
import com.luluteam.pushlib.model.DeviceInfo;
import com.luluteam.pushlib.model.UserBehaviorInfo;


/**
 * @author Guan
 * @date Created on 2018/11/21
 */
public class ContextInfoManager {


    private static ContextInfoManager instance = null;
    private static final String TAG = "ContextInfoManager";

    public static ContextInfoManager getInstance() {
        if (instance == null) {
            synchronized (ContextInfoManager.class) {
                if (instance == null) {
                    instance = new ContextInfoManager();
                }
            }
        }
        return instance;
    }

    private ContextInfoManager() {
    }

    //==========================================================


    /**
     * 将采集的context信息封装起来，然后上报服务器
     *
     * @param ctxAction
     * @param latitude
     * @param longitude
     */
    public void postContextInfo(int ctxAction, double latitude, double longitude) {
        ContextInfo info = new ContextInfo();

        info.deviceInfo = collectDeviceInfo(info);
        info.appInfo = collectPackageInfo(AppHolder.appContext);
        info.locationInfo = collectLocationInfo(latitude, longitude);
        collectOtherInfo(info, ctxAction);

        //上报信息
        Repository.postContextInfo(info);

    }

    /**
     * 将采集的context信息封装起来，然后上报服务器
     *
     * @param ctxAction
     * @param latitude
     * @param longitude
     */
    public void postContextInfo(int ctxAction, String jsonTag, double latitude, double longitude) {
        ContextInfo info = new ContextInfo();

        info.deviceInfo = collectDeviceInfo(info);
        info.appInfo = collectPackageInfo(AppHolder.appContext);
        info.locationInfo = collectLocationInfo(latitude, longitude);
        collectOtherInfo(info, ctxAction);
        info.optional.jsonTag = jsonTag;

        //上报信息
        Repository.postContextInfo(info);

    }

    /**
     * 上报情景信息，同时携带有用户行为数据，用做计算评分
     *
     * @param messageId
     * @param rating
     * @param duration
     * @param wordCount
     * @param latitude
     * @param longitude
     */
    public void postContextInfo(String messageId, int rating, long duration, int wordCount, double latitude, double longitude) {

        ContextInfo info = new ContextInfo();
        info.optional.userBehaviorInfo = collectUserBehaviorInfo(messageId, rating, duration, wordCount);
        info.appInfo = collectPackageInfo(AppHolder.appContext);
        info.deviceInfo = collectDeviceInfo(info);
        info.locationInfo = collectLocationInfo(latitude, longitude);
        collectOtherInfo(info, CtxConstant.ACTION_MSG);

        //上报信息
        Repository.postContextInfo(info);

    }


    //============================================================

    /**
     * static：采集设备信息
     *
     * @param info
     */
    private DeviceInfo collectDeviceInfo(ContextInfo info) {
        DeviceInfo deviceInfo = new DeviceInfo();
        Log.i(TAG, "收集设备信息");
        return deviceInfo;
    }


    /**
     * 采集package信息
     *
     * @param context
     */
    private ContextInfo.AppInfo collectPackageInfo(Context context) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
            if (pi != null) {
                String versionName = pi.versionName == null ? "null" : pi.versionName;
                String versionCode = pi.versionCode + "";
                ContextInfo.AppInfo appInfo = new ContextInfo.AppInfo(versionCode, versionName);
                return appInfo;
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 采集其余信息
     *
     * @param info
     */
    private void collectOtherInfo(ContextInfo info, int ctxAction) {

        info.timestamp = System.currentTimeMillis();
//        info.timestamp=1555745601000L;
        info.optional.action = ctxAction;
        info.optional.description = CtxConstant.getContextActionDescription(ctxAction);

        Log.i(TAG, "收集Other信息");
    }

    /**
     * 设置 位置信息
     *
     * @param latitude
     * @param longitude
     */
    private ContextInfo.LocationInfo collectLocationInfo(double latitude, double longitude) {
        ContextInfo.LocationInfo locationInfo = new ContextInfo.LocationInfo();
        locationInfo.latitude = latitude;
        locationInfo.longitude = longitude;
        return locationInfo;

    }


    private UserBehaviorInfo collectUserBehaviorInfo(String messageId, int rating, long duration, int wordCount) {
        UserBehaviorInfo userBehaviorInfo = new UserBehaviorInfo();
        userBehaviorInfo.messageId = messageId;
        userBehaviorInfo.rating = rating;
        userBehaviorInfo.duration = duration;
        userBehaviorInfo.wordCount = wordCount;
        return userBehaviorInfo;
    }
}
